﻿using MyCompany.MyProject.Common.DB;
using SqlSugar;

namespace MyCompany.MyProject.Common.DbSeed;

#nullable disable

public class MyContext
{
    private static MutiDBOperate connectObject => GetMainConnectionDb();
    private static string _connectionString = connectObject.Connection;
    public static string ConnId = connectObject.ConnId;
    private static DbType _dbType = (DbType)connectObject.DbType;
    private SqlSugarScope _db;

    /// <summary>
    /// 连接字符串
    /// </summary>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public static MutiDBOperate GetMainConnectionDb()
    {
        var mainConnecctDb = BaseDBConfig.MutiConnectionString.allDbs.Find(x => x.ConnId == MainDb.CurrentDbConnId);
        if (BaseDBConfig.MutiConnectionString.allDbs.Count > 0)
        {
            if (mainConnecctDb == null)
            {
                mainConnecctDb = BaseDBConfig.MutiConnectionString.allDbs[0];
            }
        }
        else
        {
            throw new Exception("请确保 appsettings.json 中配置连接字符串,并设置Enabled为true;");
        }
        return mainConnecctDb;
    }

    /// <summary>
    /// 连接字符串
    /// </summary>
    public static string ConnectionString
    {
        get { return _connectionString; }
        set {  _connectionString = value; }
    }

    /// <summary>
    /// 数据库类型
    /// </summary>
    public static DbType DbType
    {
        get { return _dbType; }
        set { _dbType = value; }
    }

    /// <summary>
    /// 数据库连接对象
    /// </summary>
    public SqlSugarScope Db
    {
        get { return _db; }
        set { _db = value; }
    }

    public MyContext(ISqlSugarClient sqlSugarClient)
    {
        if (string.IsNullOrEmpty(_connectionString))
            throw new ArgumentNullException("数据库连接字符串为空");
        _db = sqlSugarClient as SqlSugarScope;
    }
    /// <summary>
    /// 获取数据库处理对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public SimpleClient<T> GetEntityDB<T>() where T : class,new()
    {
        return new SimpleClient<T>(_db);
    }

    /// <summary>
    /// 根据实体生成数据库表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="backupTable"></param>
    /// <param name="values"></param>
    public void CreateTableByEntity<T>(bool backupTable, params T[] values) where T : class,new()
    {
        Type[] types = null;
        if(values != null)
        {
            types = new Type[values.Length];
            for(int i = 0; i < values.Length; i++)
            {
                T t = values[i];
                types[i] = typeof(T);
            }
        }
        CreateTableByEntity(backupTable, types);
    }

    /// <summary>
    /// 根据实体生成数据库表
    /// </summary>
    /// <param name="backupTable"></param>
    /// <param name="types"></param>
    public void CreateTableByEntity(bool backupTable , params Type[] types)
    {
        if(backupTable)
        {
            _db.CodeFirst.BackupTable().InitTables(types);
        }
        else
        {
            _db.CodeFirst.InitTables(types);
        }
    }

    /// <summary>
    /// 设置初始化参数
    /// </summary>
    /// <param name="connectionString"></param>
    /// <param name="dbType"></param>
    public static void Init(string connectionString,DbType dbType = DbType.SqlServer)
    {
        _connectionString = connectionString;
        _dbType = dbType;
    }

    /// <summary>
    /// 创建一个连接配置
    /// </summary>
    /// <param name="blnIsAutoCloseConnection"></param>
    /// <param name="blnIsShardSameThread"></param>
    /// <returns></returns>
    public static ConnectionConfig GetConnectionConfig(bool blnIsAutoCloseConnection = true, bool blnIsShardSameThread = false)
    {
        ConnectionConfig config = new ConnectionConfig()
        {
            ConnectionString = _connectionString,
            DbType = _dbType,
            IsAutoCloseConnection = blnIsAutoCloseConnection,
            ConfigureExternalServices = new ConfigureExternalServices()
            {

            }
        };
        return config;  
    }

    /// <summary>
    /// 获取一个自定义DB
    /// </summary>
    /// <param name="config"></param>
    /// <returns></returns>
    public static SqlSugarScope GetCustomDB(ConnectionConfig config)
    {
        return new SqlSugarScope(config);
    }

    /// <summary>
    /// 获取一个自定义数据库处理对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="scope"></param>
    /// <returns></returns>
    public static SimpleClient<T> GetCustomEntityDB<T>(SqlSugarScope scope)  where T : class, new()
    {
        return new SimpleClient<T>(scope);
    }

    /// <summary>
    /// 获取一个自定义的数据库对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="config"></param>
    /// <returns></returns>
    public static SimpleClient<T> GetCustomEntityDB<T>(ConnectionConfig config) where T : class, new()
    {
        SqlSugarScope scope = GetCustomDB(config);
        return GetCustomEntityDB<T>(scope);
    }

}
